package com.example.meituan.activity.man.frament;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.meituan.MainActivity;
import com.example.meituan.R;
import com.example.meituan.activity.bean.OrderBean;
import com.example.meituan.activity.man.ManageOrderActivity;
import com.example.meituan.activity.man.UpdateStoreActivity;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.user.MessageActivity;
import com.example.meituan.activity.user.UserMainActivity;
import com.example.meituan.activity.user.adapter.ProductAdapter;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.JiaMi;
import com.example.meituan.dao.OrderDao;
import com.example.meituan.dao.ShopDao;

import java.util.List;

public class ManageMyFragment extends Fragment {
    View rootView; // 根视图
    ShopDao shopDao;
    AdminDao adminDao;
    AdminBean currentUser;
    String path;
    String businessId;
    public void showChangePasswordDialog() {
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.update_pwd, null);
        new AlertDialog.Builder(getContext())
                .setTitle("修改密码")
                .setView(dialogView)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取用户输入的旧密码和新密码
                        EditText oldPasswordEditText = dialogView.findViewById(R.id.old_password_edit_text);
                        EditText newPasswordEditText = dialogView.findViewById(R.id.new_password_edit_text);
                        EditText tryNewText=dialogView.findViewById(R.id.new_password_edit_text_again);
                        String oldPassword = oldPasswordEditText.getText().toString();
                        String newPassword = newPasswordEditText.getText().toString();
                        String  tryNew=tryNewText.getText().toString();
                        BusinessBean businessBean=adminDao.getBusiness(businessId);
                        String encryptedPwd = JiaMi.encryptPassword(oldPassword);
                        boolean is=true;
                        if(!newPassword.equals(tryNew))
                        {
                            Toast.makeText(rootView.getContext(), "俩次密码不一致", Toast.LENGTH_SHORT).show();
                            is=false;
                        }
                        if(!businessBean.getsPwd().equals(encryptedPwd) ){
                            Toast.makeText(rootView.getContext(), "旧密码错误", Toast.LENGTH_SHORT).show();
                            is=false;
                        }
                        if(is){
                            String encryptedPwd1 = JiaMi.encryptPassword(newPassword);
                            businessBean.setsPwd(encryptedPwd1);
                            adminDao.updateStore(businessBean);
                            SharedPreferences sharedPreferences = rootView.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.clear();
                            editor.apply();
                            ((MainActivity) rootView.getContext()).finish(); // 结束当前 Activity
                            // 启动登录界面
                            Intent intent = new Intent(rootView.getContext(), MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); // 确保回到登录界面时清空任务栈
                            rootView.getContext().startActivity(intent);
                        }
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        rootView = inflater.inflate(R.layout.fragment_manage_my, container, false);
        if(adminDao==null){
            adminDao=new AdminDao();
        }
        if(shopDao==null){
            shopDao=new ShopDao(getActivity());
        }
        currentUser = adminDao.getUserInfo(rootView.getContext());
        businessId=currentUser.getUserId();

        BusinessBean businessBean=adminDao.getBusiness(businessId);
        ImageView img=rootView.findViewById(R.id.manage_my_img);
        TextView idText=rootView.findViewById(R.id.manage_my_id);
        TextView nameText=rootView.findViewById(R.id.manage_my_name);
        TextView desText=rootView.findViewById(R.id.manage_my_des);
        idText.setText("id:"+businessId);
        nameText.setText(businessBean.getsName());
        desText.setText("店铺简介:"+businessBean.getsDescribe());
        path = businessBean.getsImg();
        Bitmap bitmap= BitmapFactory.decodeFile(path);
        img.setImageBitmap(bitmap);
        TextView exit = rootView.findViewById(R.id.manage_my_exit);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(rootView.getContext());
                alertDialog.setTitle("退出登录");
                alertDialog.setMessage("确定退出登录");
                alertDialog.setCancelable(false);
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences sharedPreferences = container.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.clear();
                        editor.apply();

                        // 启动登录界面
                        Intent intent = new Intent(container.getContext(), MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); // 确保回到登录界面时清空任务栈
                        container.getContext().startActivity(intent);
                    }
                });
                AlertDialog alertDialog1=alertDialog.create();
                alertDialog1.show();
            }

        });
        //注销账号
        TextView delete=rootView.findViewById(R.id.manage_my_delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(rootView.getContext());
                alertDialog.setTitle("注销账号");
                alertDialog.setMessage("确定注销该账号");
                alertDialog.setCancelable(false);
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        List<ShopBean> shopBeans=shopDao.getAllShopsByBusinessId(businessId);
                        List<OrderBean> orderBeans= OrderDao.getAllOrdersByBusinessId(businessId);
                        if ((shopBeans == null || shopBeans.isEmpty() )&& (orderBeans.isEmpty() || orderBeans==null)){
                            int a=adminDao.deleteBusinesssById(businessId);
                            if(a==1){
                                Toast.makeText(rootView.getContext(), "注销成功", Toast.LENGTH_SHORT).show();
                                SharedPreferences sharedPreferences = container.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.clear();
                                editor.apply();

                                // 启动登录界面
                                Intent intent = new Intent(container.getContext(), MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); // 确保回到登录界面时清空任务栈
                                container.getContext().startActivity(intent);
                            }
                            else {
                                Toast.makeText(rootView.getContext(), "注销失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                        else {
                            Toast.makeText(rootView.getContext(), "店铺中有商品,不能实现注销", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                AlertDialog alertDialog1=alertDialog.create();
                alertDialog1.show();

            }

        });

        //修改店铺信息
        TextView update=rootView.findViewById(R.id.update_man_inf);
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(rootView.getContext(), UpdateStoreActivity.class);
                startActivity(intent);

            }
        });
        //修改密码
        TextView changePasswordButton = rootView.findViewById(R.id.update_man_pwd);
        changePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChangePasswordDialog();

            }
        });

        //订单管理
        com.google.android.material.button.MaterialButton button=rootView.findViewById(R.id.manage_order);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(rootView.getContext(), ManageOrderActivity.class);
                startActivity(intent);

            }
        });

        //评论管理
        com.google.android.material.button.MaterialButton button1=rootView.findViewById(R.id.talk_manage);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(inflater.getContext(), MessageActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("ifUser", "business");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        return rootView;
    }



}